package io.vincentwinner.systeminfoapi.api;

import com.alibaba.fastjson.JSONObject;
import io.vincentwinner.systeminfoapi.dao.IDiskDao;
import io.vincentwinner.systeminfoapi.service.IDynamicInfoService;
import io.vincentwinner.systeminfoapi.service.IStaticInfoService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/disk")
public class DiskController {

    @Resource(name = "staticInfoService")
    private IStaticInfoService staticInfoService;

    @Resource(name = "dynamicInfoService")
    private IDynamicInfoService dynamicInfoService;

    @Resource(name = "diskDao")
    private IDiskDao diskDao;

    @RequestMapping
    public Object root(){
        JSONObject jsonObject = new JSONObject(2);
        jsonObject.put("staticDiskInfo",staticInfoService.getDiskStaticInfo());
        jsonObject.put("dynamicDiskInfo",dynamicInfoService.getDiskDynamicInfo());
        return jsonObject;
    }

    @RequestMapping("/static")
    public Object static$(){
        return staticInfoService.getDiskStaticInfo();
    }

    @RequestMapping("/dynamic")
    public Object dynamic(){
        return dynamicInfoService.getDiskDynamicInfo();
    }

    @RequestMapping("/diskNum")
    public Object diskNum(){
        return diskDao.getDiskNum();
    }

    @RequestMapping("/totalSpace/{diskNum}")
    public Object totalSpace(@PathVariable("diskNum")Integer diskNum){
        if(diskNum > diskDao.getDiskNum()) return 0;
        return diskDao.getTotalSpace(diskNum);
    }

    @RequestMapping("/freeSpace/{diskNum}")
    public Object freeSpace(@PathVariable("diskNum")Integer diskNum){
        if(diskNum > diskDao.getDiskNum()) return 0;
        return diskDao.getFreeSpace(diskNum);
    }

    @RequestMapping("/usedSpace/{diskNum}")
    public Object usedSpace(@PathVariable("diskNum")Integer diskNum){
        if(diskNum > diskDao.getDiskNum()) return 0;
        return diskDao.getUsedSpace(diskNum);
    }

    @RequestMapping("/usage/{diskNum}")
    public Object usage(@PathVariable("diskNum")Integer diskNum){
        if(diskNum > diskDao.getDiskNum()) return 0;
        return diskDao.getUsage(diskNum);
    }

}
